import cv2

# 读取图像
img = cv2.imread("tk.jpeg", 0)  # 0表示读取为灰度图像

# 全局直方图均衡化
imgEqu = cv2.equalizeHist(img)

# 创建CLAHE对象
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(3, 3))
# 自适应局部直方图均衡化
imgLocalEqu = clahe.apply(img)

# 设置显示的图像大小
width = 400
height = 400
dim = (width, height)

# 调整图像大小
img_resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
imgEqu_resized = cv2.resize(imgEqu, dim, interpolation=cv2.INTER_AREA)
imgLocalEqu_resized = cv2.resize(imgLocalEqu, dim, interpolation=cv2.INTER_AREA)

# 显示图像
cv2.imshow('Original Image', img_resized)
cv2.imshow('Global Histogram Equalization', imgEqu_resized)
cv2.imshow('Local Histogram Equalization', imgLocalEqu_resized)

# 等待按键后关闭所有窗口
cv2.waitKey(0)
cv2.destroyAllWindows()